package com.feature.shop.front.product.mq;

import java.util.HashMap;
import java.util.Map;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RabbitMqConfig {
    public static final String INFO_UPDATE_EXCHANGE_NAME = "infoUpdateExchange";
    public static final String INFO_UPDATE_QUEUE = "infoUpdateQueue";
    public static final String INFO_UPDATE_ROUTING_KEY = "infoUpdateRoutingKey";

    public static final String PRODCUT_SPEC_INVENTORY_EXCHANGE_NAME = "PRODCUT_SPEC_INVENTORY_EXCHANGE_NAME";
    public static final String PRODCUT_SPEC_INVENTORY_QUEUE_ONE = "PRODCUT_SPEC_INVENTORY_QUEUE_ONE";
    public static final String PRODCUT_SPEC_INVENTORY_QUEUE_TWO = "PRODCUT_SPEC_INVENTORY_QUEUE_ONE";
    public static final String PRODCUT_SPEC_INVENTORY_ROUTING_KEY_PRE = "PRODCUT_SPEC_INVENTORY_ROUTING_KEY_";

    public static final String INBOUND_ORDER_EXCHANGE_NAME = "INBOUND_ORDER_EXCHANGE_NAME";
    public static final String INBOUND_ORDER_QUEUE_ONE = "INBOUND_ORDER_QUEUE_ONE";
    public static final String INBOUND_ORDER_QUEUE_TWO = "INBOUND_ORDER_QUEUE_TWO";
    public static final String INBOUND_ORDER_ROUTING_KEY_PRE = "INBOUND_ORDER_ROUTING_KEY_";

    @Bean
    DirectExchange infoUpdateExchange() {
        return new DirectExchange(INFO_UPDATE_EXCHANGE_NAME);
    }

    @Bean
    Queue infoUpdateQueue() {
        return new Queue(INFO_UPDATE_QUEUE, true, false, false);
    }

    @Bean
    public Binding infoUpdateBinding() {
        return BindingBuilder.bind(infoUpdateQueue()).to(infoUpdateExchange()).with(INFO_UPDATE_ROUTING_KEY);
    }

    @Bean
    DirectExchange inboundOrderExchange() {
        return new DirectExchange(INBOUND_ORDER_EXCHANGE_NAME);
    }

    @Bean
    Queue inboundOrderQueue1() {
        Map<String, Object> args = new HashMap<>();
        args.put("x-single-active-consumer", true);
        return new Queue(INBOUND_ORDER_QUEUE_ONE, true, false, false, args);
    }

    @Bean
    Queue inboundOrderQueue2() {
        Map<String, Object> args = new HashMap<>();
        args.put("x-single-active-consumer", true);
        return new Queue(INBOUND_ORDER_QUEUE_TWO, true, false, false, args);
    }

    @Bean
    public Binding inboundOrderQueue1Binding() {
        return BindingBuilder.bind(inboundOrderQueue1()).to(inboundOrderExchange()).with(INBOUND_ORDER_ROUTING_KEY_PRE + '0');
    }

    @Bean
    public Binding inboundOrderQueue2Binding() {
        return BindingBuilder.bind(inboundOrderQueue2()).to(inboundOrderExchange()).with(INBOUND_ORDER_ROUTING_KEY_PRE + '1');
    }

    @Bean
    DirectExchange prodcutSpecInventoryExchange() {
        return new DirectExchange(PRODCUT_SPEC_INVENTORY_EXCHANGE_NAME);
    }

    @Bean
    Queue prodcutSpecInventoryQueue1() {
        Map<String, Object> args = new HashMap<>();
        args.put("x-single-active-consumer", true);
        return new Queue(PRODCUT_SPEC_INVENTORY_QUEUE_ONE, true, false, false, args);
    }

    @Bean
    Queue prodcutSpecInventoryQueue2() {
        Map<String, Object> args = new HashMap<>();
        args.put("x-single-active-consumer", true);
        return new Queue(PRODCUT_SPEC_INVENTORY_QUEUE_TWO, true, false, false, args);
    }

    @Bean
    public Binding prodcutSpecInventoryQueue1Binding() {
        return BindingBuilder.bind(prodcutSpecInventoryQueue1()).to(prodcutSpecInventoryExchange()).with(PRODCUT_SPEC_INVENTORY_ROUTING_KEY_PRE + '0');
    }

    @Bean
    public Binding prodcutSpecInventoryQueue2Binding() {
        return BindingBuilder.bind(prodcutSpecInventoryQueue2()).to(prodcutSpecInventoryExchange()).with(PRODCUT_SPEC_INVENTORY_ROUTING_KEY_PRE + '1');
    }
}
